package thread;

import java.util.ArrayList;

public class Test {
    public static void main(String[] args) {
        ArrayList<Integer> list =new ArrayList<>();
Object object=new Object();
       Thread te=new Thread("莴笋老二"){
            @Override
            public void run() {
                for (int i = 0; i < 1000; i++) {
                   synchronized (object){
                       try {
                           object.wait(1000);
                           System.out.println("停了1秒");
                       } catch (InterruptedException e) {
                           e.printStackTrace();
                       }
                       System.out.println("莴笋加了一个");
                       list.add(i);};
                }
                System.out.println(this.getName()+":"+list.size());
            }
        };
        Thread te2=new Thread("马化腾2"){
            @Override
            public void run() {
                for (int i = 0; i < 1000; i++) {
                    synchronized(object){
                        try {
                            object.notify();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        list.add(i);};
                }
                System.out.println(getName()+":"+list.size());
            }
        };
        te.start();
        te2.start();

        System.out.println(Thread.currentThread().getName()+":"+list.size());
    }
}
